#!/bin/bash

# Disallow use of uninitialized variables.
set -o nounset

# Exit if any task fails.
#set -o errexit

# Exit if a task in pipeline fails.
set -o pipefail

JAR=jpm-0.3.jar


function run_tests {
    local dir=$1
    local fileFilter=$2
    local options=${3:-}
    pushd $dir > /dev/null 2> /dev/null
    files=$(ls $fileFilter*.expected 2> /dev/null | sed s/\.expected//)
    popd > /dev/null 2> /dev/null

    for f in $files; do
        java -jar $JAR --indir=$dir --infile=$f.jpm --outdir=../_Output --nologo --diag=nofilename $options 2> $f.out
        # TODO: add check whether the translation succeeded or not. if $?==1 then outout contains errors. 
        
        echo $f.jpm . . .
        diff -b $dir/$f.expected $f.out
        #rm out
    done
}

cd ../dist

testName=${1:-?}

echo '---------------------'
echo Parser tests
echo '---------------------'
testPath=../Tests/Parser
run_tests $testPath $testName --parseonly


echo -e '\n---------------------'
echo Semantic analyzer tests...
echo '---------------------'
testPath=../Tests/Analyzer
run_tests $testPath $testName 

